<!-- 新建任务 -->
<template>
    <div class="addHomework">
        <input
            autofocus="true"
            type="text"
            v-model="newHomework.homeworkName"
            placeholder="作业名称">

        <input
            type="date"
            v-model="newHomework.deadLine">

        <textarea
            cols="38"
            rows="10"
            v-model="newHomework.homeworkDetail"
            placeholder="作业详情">
        </textarea>

        <a class="submit" @click="addHomework()">提交</a>
    </div>
</template>

<style lang="sass">
    .addHomework{
        input{
            color: #39f;
            font-size: 18px;
            margin-bottom: 20px;
            width:80%;
        }
        .new{
            margin-top:20px;
        }
    }
</style>

<script>
    export default{
        props: ['homeworks'],
        data(){
            return {
                newHomework:{
                    homeworkName:'',
                    homeworkDetail:'',
                    deadLine:'',
                    setDate: (new Date()).toISOString(),
                    inCharge:'',
                }
            }
        },
        ready(){
            this.$http.get('../user/detail').then(function (d) {
                this.newHomework.inCharge = d.data.username;
            }, function (err) {
                console.log(err)
            })
        },

        methods:{
            addHomework(){
                this.newHomework.homeworkName = (this.newHomework.homeworkName).trim();
                this.newHomework.homeworkDetail = (this.newHomework.homeworkDetail).trim();
                if (this.newHomework.homeworkName && this.newHomework.homeworkDetail) {
                    this.$http.post('/admin/work/newWork', this.newHomework).then(function (d) {
                        if (d.data === 'success') {
                            alert('新建作业成功');
                            this.homeworks.push(this.newHomework);
                            this.newHomework = {};
                        }else if (d.data === 'SameName') {
                            alert('与现有作业重名');
                            var homeworkName = document.getElementsByTagName('input')[0];
                            homeworkName.focus();
                        }else if (d.data == 'Empty') {
                            alert('作业名或详情为空')
                        }
                    }, function (err) {
                        console.log(err);
                    });
                }
                else{
                    alert('作业名或详情为空')
                }

            },
        }
    }
</script>
